package com.baizw.cultivation.module.admin.service.impl;

import com.baizw.cultivation.common.page.CustomPage;
import com.baizw.cultivation.module.admin.entity.RoleEntity;
import com.baizw.cultivation.module.admin.entity.vo.Permission;
import com.baizw.cultivation.module.admin.mapper.RoleMapper;
import com.baizw.cultivation.module.admin.service.RoleService;
import com.baizw.cultivation.util.PermissionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 角色业务接口实现
 *
 * @author 剧终1204
 * @version V1.0
 * @date 2020年04月27日 11:44:59
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, RoleEntity> implements RoleService {

    @Override
    @Cacheable(cacheNames = "role")
    public CustomPage<RoleEntity> listRole(CustomPage<?> page) {
        CustomPage<RoleEntity> customPage = baseMapper.listRole(page);
        List<RoleEntity> records = customPage.getRecords();
        for (RoleEntity record : records) {
            List<Permission> permissionList = record.getPermissions();
            for (Permission permission : permissionList) {
                permission.setActionEntitySet(PermissionUtil.convert(permission.getPermission()));
            }
        }
        return customPage;
    }

    @Override
    @Cacheable(cacheNames = "sys_role", key = "#roleId")
    public RoleEntity findById(Long roleId) {
        RoleEntity roleEntity = baseMapper.findById(roleId);
        List<Permission> permissionList = roleEntity.getPermissions();
        for (Permission permission : permissionList) {
            permission.setActionEntitySet(PermissionUtil.convert(permission.getPermission()));
        }
        return roleEntity;
    }
}
